package aplicacion;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JRadioButton;
import javax.swing.JSplitPane;
import javax.swing.JTable;
import javax.swing.JTextField;

import net.miginfocom.swing.MigLayout;

import javax.swing.JScrollPane;
import javax.swing.table.DefaultTableModel;

import dominio.Hotel;
import dominio.PaqueteDeViaje;

import java.io.BufferedInputStream;
import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;

@SuppressWarnings("serial")
public class MenuViaje extends JPanel {
	private JPanel panelIzquierda;
	private JPanel panelCentro;
	
	public MenuViaje() {
		setLayout( new BorderLayout() );
		//cargarPanelArriba();
		cargarPanelIzquierda();
		cargarPanelMedio();
		
	}
	
	public void cargarPanelIzquierda(){
		panelIzquierda = new CargarPaqueteViaje().getPanel();
		add(panelIzquierda, BorderLayout.WEST);
	}
	
	public void cargarPanelMedio(){
		panelCentro = new ElegirVuelo().getPanel();
		add(panelCentro, BorderLayout.CENTER);
	}

	public JPanel getPanel() {
		return this;
	}

}

class ElegirVuelo {
	private JPanel panelPrincipal;
	private JSplitPane panelTabla;
	private JPanel panelTablaIda;
	private JPanel panelTablaVuelta;
	private JTable tbIda;
	private JTable tbVuelta;
	
	public ElegirVuelo() {
		panelPrincipal = new JPanel();
		panelPrincipal.setLayout(new BorderLayout());
		
		cargarTablaIda();
		cargarTablaVuelta();
		panelTabla = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
		panelTabla.setResizeWeight(.5d);
		panelTabla.setTopComponent(panelTablaIda);
		panelTabla.setBottomComponent(panelTablaVuelta);
		panelPrincipal.add(panelTabla, BorderLayout.CENTER);
	}

	public JPanel getpanelPrincipal() {
		return panelPrincipal;
	}

	public void setpanelPrincipal(JPanel panelPrincipal) {
		this.panelPrincipal = panelPrincipal;
	}
	
	public void cargarTablaIda(){
		panelTablaIda = new JPanel();
		JLabel lblIda = new JLabel("IDA");
		JButton btnSeleccionar = new JButton("Seleccionar");
		panelTablaIda.setLayout( new BorderLayout() );
		String[] columnNames = {"Pais","Ciudad",
				"Fecha","Hora Salida","Hora Llegada",};
		tbIda = new JTable(new DefaultTableModel(null, columnNames));
		try {
			agregarFilaTablaIda();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		JScrollPane scrollPane = new JScrollPane(tbIda);
		panelTablaIda.add(lblIda, BorderLayout.NORTH);
		panelTablaIda.add(scrollPane, BorderLayout.CENTER);
		panelTablaIda.add(btnSeleccionar, BorderLayout.SOUTH);
	}
	
	void agregarFilaTablaIda() throws ClassNotFoundException, IOException{
		File file = new File("bin/aplicacion/archivos/vuelosIda.txt");
		if (file.exists()){
			DefaultTableModel defaultModel = (DefaultTableModel) tbIda.getModel();  
			ObjectInputStream ois = null;
			try{
				ois = new ObjectInputStream(new BufferedInputStream(new FileInputStream(file)));
				while(true){
					PaqueteDeViaje pdv = (PaqueteDeViaje) ois.readObject();
					defaultModel.addRow(new Object[]{ pdv.getAeropuertoSalida(),
													pdv.getAeropuertoSalida(),
													pdv.getFechaSalida(),
													pdv.getHoraSalida(),
													pdv.getHoraArrivo(),});
					}
			}catch(EOFException e){
				
			}finally{
				if(ois != null){
					ois.close();
				}
			}
			tbIda = new JTable(defaultModel);
		}   
	}
	
	public void cargarTablaVuelta(){
		panelTablaVuelta = new JPanel();
		JLabel lblVuelta = new JLabel("VUELTA");
		JButton btnSeleccionar = new JButton("Seleccionar");
		panelTablaVuelta.setLayout( new BorderLayout() );
		String[] columnNames = {"Pais","Ciudad",
				"Fecha","Hora Salida","Hora Llegada",};
		tbVuelta = new JTable(new DefaultTableModel(null, columnNames));
		try {
			agregarFilaTablaIda();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		JScrollPane scrollPane = new JScrollPane(tbVuelta);
		panelTablaVuelta.add(lblVuelta, BorderLayout.NORTH);
		panelTablaVuelta.add(scrollPane, BorderLayout.CENTER);
		panelTablaVuelta.add(btnSeleccionar, BorderLayout.SOUTH);
	}
	
	void agregarFilaTablaVuelta() throws ClassNotFoundException, IOException{
		File file = new File("bin/aplicacion/archivos/vuelosVuelta.txt");
		if (file.exists()){
			DefaultTableModel defaultModel = (DefaultTableModel) tbVuelta.getModel();  
			ObjectInputStream ois = null;
			try{
				ois = new ObjectInputStream(new BufferedInputStream(new FileInputStream(file)));
				while(true){
					PaqueteDeViaje pdv = (PaqueteDeViaje) ois.readObject();
					defaultModel.addRow(new Object[]{ pdv.getAuropuertaArrivo(),
													pdv.getAuropuertaArrivo(),
													pdv.getFechaSalida(),
													pdv.getHoraSalida(),
													pdv.getHoraArrivo(),});
					}
			}catch(EOFException e){
				
			}finally{
				if(ois != null){
					ois.close();
				}
			}
			tbVuelta = new JTable(defaultModel);
		}   
	}

	public JPanel getPanel() {
		// TODO Auto-generated method stub
		return panelPrincipal;
	}
	
}

class ElegirHotel {
	private JPanel panelPrincipal;
	private JPanel panelTablaHoteles;
	private JTable tbHoteles;
	
	public ElegirHotel() {
		panelPrincipal = new JPanel();
		panelPrincipal.setLayout(new BorderLayout());		
		cargarTablaHoteles();
	}
	
	public JPanel getpanelPrincipal() {
		return panelPrincipal;
	}

	public void setpanelPrincipal(JPanel panelPrincipal) {
		this.panelPrincipal = panelPrincipal;
	}
	
	public void cargarTablaHoteles(){
		panelTablaHoteles = new JPanel();
		panelTablaHoteles.setLayout( new GridLayout() );
		panelPrincipal.add(panelTablaHoteles, BorderLayout.CENTER);
		String[] columnNames = {"Id Hotel","Nombre",
				"Pais","Provincia","Ciudad","Calle","Nro","Capacidad",};
		tbHoteles = new JTable(new DefaultTableModel(null, columnNames));
		try {
			agregarFilaTablaIda();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		JScrollPane scrollPane = new JScrollPane(tbHoteles);
		panelTablaHoteles.add(scrollPane);		
	}
	
	void agregarFilaTablaIda() throws ClassNotFoundException, IOException{
		File file = new File("bin/aplicacion/archivos/hoteles.txt");
		if (file.exists()){
			DefaultTableModel defaultModel = (DefaultTableModel) tbHoteles.getModel();  
			ObjectInputStream ois = null;
			try{
				ois = new ObjectInputStream(new BufferedInputStream(new FileInputStream(file)));
				while(true){
					Hotel h = (Hotel) ois.readObject();
					defaultModel.addRow(new Object[]{ h.getIdHotel(),
												/*	h.getNombreHotel(),
													h.getDireccionHotel().getPais(),
													h.getDireccionHotel().getProvincia(),
													h.getDireccionHotel().getCiudad(),
													h.getDireccionHotel().getCalle(),
													h.getDireccionHotel().getNumero(),
													h.getCapacidadHotel(), 	*/});
					}
			}catch(EOFException e){
				
			}finally{
				if(ois != null){
					ois.close();
				}
			}
			tbHoteles = new JTable(defaultModel);
		}   
	}

}

class CargarPaqueteViaje {
	private JPanel panel;
	private JTextField txtFechaIda;
	private JTextField txtFechaVuelta;
	
	@SuppressWarnings("rawtypes")
	public CargarPaqueteViaje() {
		panel = new JPanel();
		panel.setLayout(new MigLayout());
		
		JLabel lblPaquetes = new JLabel("Paquetes");
		lblPaquetes.setBounds(0, 0, 450, 14);
		panel.add(lblPaquetes, "cell 0 0");
		
		JRadioButton rdbtnSoloVuelo = new JRadioButton("Solo Vuelo");
		rdbtnSoloVuelo.setBounds(10, 21, 109, 23);
		panel.add(rdbtnSoloVuelo, "cell 0 1, gapleft 15");
		
		JRadioButton rdbtnVueloMasHotel = new JRadioButton("Vuelo + Hotel");
		rdbtnVueloMasHotel.setBounds(137, 21, 109, 23);
		panel.add(rdbtnVueloMasHotel, "cell 0 1");
		
		JLabel lblOrigen = new JLabel("Origen");
		lblOrigen.setBounds(20, 51, 46, 14);
		panel.add(lblOrigen, "cell 0 2");
		
		JComboBox cbxPaisOrigen = new JComboBox();
		cbxPaisOrigen.setMinimumSize(new Dimension(190,20));
		panel.add(cbxPaisOrigen, "cell 0 3, gapleft 15");
		
		JComboBox cbxProvinciaOrigen = new JComboBox();
		cbxProvinciaOrigen.setMinimumSize(new Dimension(190,20));
		panel.add(cbxProvinciaOrigen, "cell 0 4, gapleft 15");
		
		JComboBox cbxCiudadOrigen = new JComboBox();
		cbxCiudadOrigen.setMinimumSize(new Dimension(190,20));
		panel.add(cbxCiudadOrigen, "cell 0 5, gapleft 15");
		
		JLabel lblDestino = new JLabel("Destino");
		lblDestino.setBounds(233, 51, 46, 14);
		panel.add(lblDestino, "cell 0 6");
		
		JComboBox cbxPaisDestino = new JComboBox();
		cbxPaisDestino.setMinimumSize(new Dimension(190,20));
		panel.add(cbxPaisDestino, "cell 0 7, gapleft 15");
		
		JComboBox cbxProvinciaDestino = new JComboBox();
		cbxProvinciaDestino.setMinimumSize(new Dimension(190,20));
		panel.add(cbxProvinciaDestino, "cell 0 8, gapleft 15");
		
		JComboBox cbxCiudadDestino = new JComboBox();
		cbxCiudadDestino.setMinimumSize(new Dimension(190,20));
		panel.add(cbxCiudadDestino, "cell 0 9, gapleft 15");
		
		JLabel lblIda = new JLabel("Ida");
		lblIda.setBounds(20, 178, 46, 14);
		panel.add(lblIda, "cell 0 10");
		
		JLabel lblVuelta = new JLabel("Vuelta");
		lblVuelta.setBounds(233, 178, 46, 14);
		panel.add(lblVuelta, "cell 0 10, gapleft 110");
		
		txtFechaIda = new JTextField();
		txtFechaIda.setText("dd/mm/aaaa");
		txtFechaIda.setColumns(10);
		panel.add(txtFechaIda, "cell 0 11, gapleft 10");
	
		
		txtFechaVuelta = new JTextField();
		txtFechaVuelta.setText("dd/mm/aaaa");
		txtFechaVuelta.setColumns(10);
		panel.add(txtFechaVuelta, "cell 0 11, gapleft 10");
		
		
		JLabel lblCantidadHabitaciones = new JLabel("Habitaciones:");
		lblCantidadHabitaciones.setBounds(30, 272, 46, 14);
		panel.add(lblCantidadHabitaciones, "cell 0 13");
		
		JComboBox cbxCantidadHabitaciones = new JComboBox();
		cbxCantidadHabitaciones.setMinimumSize(new Dimension(190,20));;
		panel.add(cbxCantidadHabitaciones, "cell 0 14, gapleft 10");	
		
		JButton btnBuscar = new JButton("Buscar");
		panel.add(btnBuscar, "cell 0 15, alignx right");
		
	}

	public JPanel getPanel() {
		return panel;
	}

	public void setPanel(JPanel panel) {
		this.panel = panel;
	}
}

